package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.apis.wemedia.IWeMediaClient;
import com.heima.common.constants.UserCheckConstants;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.dtos.ApUserAuthDto;
import com.heima.model.user.dtos.ApUserPassDto;
import com.heima.model.user.pojos.ApUser;
import com.heima.model.user.pojos.ApUserRealname;
import com.heima.user.mapper.ApUserAuthMapper;
import com.heima.user.mapper.ApUserMapper;
import com.heima.user.service.ApUserAuthService;
import com.heima.user.service.ApUserService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class ApUserAuthServiceImpl extends ServiceImpl<ApUserAuthMapper, ApUserRealname> implements ApUserAuthService {
    @Autowired
    private ApUserService service;
    @Autowired
    private RabbitTemplate template;
    @Override
    public ResponseResult authList(ApUserAuthDto dto) {
        //检查参数
        dto.checkParam();
        //分页查询
        IPage page=new Page(dto.getPage(),dto.getSize());
        //构建分页查询条件   根据状态查询
        LambdaQueryWrapper<ApUserRealname> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(dto.getStatus()!=null,ApUserRealname::getStatus,dto.getStatus());
        page(page,wrapper);
        ResponseResult result=new PageResponseResult(dto.getPage(),dto.getSize(), Math.toIntExact(page.getTotal()));
        result.setData(page.getRecords());
        return result;
    }

    /**
     * 审核失败
     * @param dto
     * @return
     */
    @Override
    public ResponseResult authFail(ApUserPassDto dto) {
        //参数校验
        if (dto==null||dto.getId()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        LambdaUpdateWrapper<ApUserRealname> wrapper=new LambdaUpdateWrapper<>();
        wrapper.eq(ApUserRealname::getId,dto.getId())
                .set(ApUserRealname::getStatus, UserCheckConstants.STATUS_FAIL)
                .set(ApUserRealname::getReason,dto.getMsg())
                .set(ApUserRealname::getUpdatedTime,new Date());
        update(wrapper);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
    /**
     * 审核通过
     * @param dto
     * @return
     */
    @Autowired
    private IWeMediaClient client;
    @Override
    public ResponseResult authPass(ApUserPassDto dto) {
        //参数校验
        if (dto==null||dto.getId()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        LambdaUpdateWrapper<ApUserRealname> wrapper=new LambdaUpdateWrapper<>();
        wrapper.eq(ApUserRealname::getId,dto.getId())
                .set(ApUserRealname::getStatus, UserCheckConstants.STATUS_PASS)
                .set(ApUserRealname::getUpdatedTime,new Date());
        update(wrapper);
        //异步调用自媒体端
        //根据id查询APP用户
        LambdaQueryWrapper<ApUserRealname> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(ApUserRealname::getId,dto.getId());
        ApUserRealname apUserRealname = getOne(queryWrapper);
        //根据用户id获得用户
        Integer userId = apUserRealname.getUserId();
        ApUser apUser = service.getById(userId);
        //template.convertAndSend("ap_add_direct","ap_add",apUser);
        //使用feign客户端远程调用
        ResponseResult responseResult = client.saveWeMediaUser(apUser);
        if (responseResult.getCode()!=200){
            throw new RuntimeException("添加自媒体用户失败");
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
